package com.info33.platform.business.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.info33.platform.business.app.dto.AppProtocolDTO;
import com.info33.platform.business.app.entity.AppProtocol;
import com.info33.platform.business.app.service.AppProtocolService;
import com.info33.platform.common.constant.TableConstant;
import com.info33.platform.common.mvc.controller.BaseController;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.common.util.SysUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author admin
 */
@RestController
@RequestMapping("/AppProtocol")
@Api(tags = "app - 协议")
public class AppProtocolController extends BaseController<AppProtocolService, AppProtocol> {

    private final AppProtocolService appProtocolService;

    @Autowired
    public AppProtocolController(AppProtocolService appProtocolService) {
        super(appProtocolService);
        this.appProtocolService = appProtocolService;
    }

    @PostMapping(value = "/sc/one")
    @ApiOperation(value = "app 获取最高序列协议", notes = "两个 app 通用")
    public Result<AppProtocol> publicOne(
            @Validated(value = AppProtocolDTO.PublicPage.class) @RequestBody AppProtocolDTO dto) {
        return Result.ok(appProtocolService.getOne(new QueryWrapper<AppProtocol>()
                .eq("type", dto.getType())
                .orderByAsc(TableConstant.SORT)
                .orderByDesc(TableConstant.CREATE_DATE)
                .last(TableConstant.LIMIT_ONE)));
    }

    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     * pc
     *
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    @PostMapping(value = "/pc/list/{pageNum}/{pageSize}")
    @ApiOperation(value = "pc - 分页")
    public Result<IPage<AppProtocol>> pcPage(
            @RequestBody AppProtocolDTO dto,
            @PathVariable int pageNum,
            @PathVariable int pageSize) {
        return Result.ok(appProtocolService.pcPage(new Page<>(pageNum, pageSize), dto, getCurrentUser()));
    }

    @PostMapping(value = "/pc/add")
    @ApiOperation(value = "pc - 添加")
    public Result<Object> pcAdd(
            @RequestBody AppProtocol entity) {
        if (Boolean.FALSE.equals(SysUtil.isSysAdmin())) {
            return Result.error(Result.RESULT_ERROR_NO_POWER);
        }
        return appProtocolService.save(entity) ? Result.ok() : Result.error();
    }

    @PutMapping(value = "/pc/edit")
    @ApiOperation(value = "pc - 编辑")
    public Result<Object> pcEdit(
            @RequestBody AppProtocol appProtocol) {
        return appProtocolService.pcEdit(appProtocol);
    }

    @DeleteMapping(value = "/pc/remove/{id}")
    @ApiOperation(value = "pc - 删除")
    public Result<Object> pcRemove(
            @PathVariable(value = "id") Long id) {
        return appProtocolService.pcRemove(id);
    }

}